package androidApplication.javaFiles;


import java.util.ArrayList;
import java.util.Vector;

import ChatPackage.ChatHandler;
import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;



public class UsersNameList extends ListActivity { 

	public static String noChats = "No chats yet... click an icon on map to start chat";
	private ArrayList<String> _names;
	/** Called when the activity is first created. */

		public void onCreate(Bundle icicle) {
			super.onCreate(icicle);
			// Create an array of Strings, that will be put to our ListActivity
			_names = new ArrayList<String> ();
			initStrArray();
			// Create an ArrayAdapter, that will actually make the Strings above
			// appear in the ListView
			this.setListAdapter(new ArrayAdapter<String>(this,
					android.R.layout.simple_list_item_1, _names));
		}
		
		protected void initStrArray(){
			Vector<String> strArray = ChatHandler.getUsersList();
			
			if (strArray.size()==0){
				Log.v("UseListersNam", noChats);
				addUser(noChats);
			}
			else{
				for (int i = 0; i < strArray.size(); i++) {
					if(strArray.elementAt(i) != null)
						addUser(strArray.elementAt(i));
				}
			}
		}
		
		protected void addUser(String userName) {
			this._names.add(userName);		
		}

		@Override
		protected void onListItemClick(ListView l, View v, int position, long id) {
			super.onListItemClick(l, v, position, id);
			// Get the item that was clicked
			Object o = this.getListAdapter().getItem(position);
			String userName = o.toString();
			if (!userName.equals(noChats)){
				Toast.makeText(this, "You selected: " + userName, Toast.LENGTH_LONG)
						.show();
				ChatHandler.startChat(userName, this);
			}
		}
	}
